package model;

import item.Item;

import java.util.*;

import control.*;
import unit.*;
import util.*;

public class Map implements Tick {

	private Tile[][] tiles;
	private java.util.Map<Integer, Player> players;
	private java.util.Map<Integer, Npc> npcs;

	public Map(Tile[][] tiles) {
		this.tiles = tiles;
		players = new HashMap<Integer,Player>();
		npcs = new HashMap<Integer,Npc>();
	}

	public Tile getTile(IntPoint point) {
		return tiles[point.getX()][point.getY()];
	}

	public java.util.Map<Integer,Player> getPlayers() {
		return players;
	}

	public java.util.Map<Integer,Npc> getNpcs() {
		return npcs;
	}

	public boolean addUnit(Unit unit) {
		if (unit instanceof Player) {
			players.put(unit.id(),(Player) unit);
			return true;
		} else if (unit instanceof Npc) {
			npcs.put(unit.id(),(Npc) unit);
			return true;
		}
		System.out.println("Unit is wrong /Map");
		return false;
	}
	
	
	public boolean hasUnitByID(int id){
		if(players.containsKey(id)){
			return true;
		}else if(npcs.containsKey(id)){
			return true;
		}
		return false;
	}
	public Unit getUnitByID(int id){
		if(players.containsKey(id)){
			return players.get(id);
		}else if(npcs.containsKey(id)){
			return npcs.get(id);
		}
		System.out.println("Do not contain unit! //Map");
		return null;
	}
	
	/*
	 * public boolean contains(Unit unit){ if(players.contains(unit)){ return
	 * true; }else if(npcs.contains(unit)){ return true; }else{ return false; }
	 * }
	 */

	public void tick() {
		for(Player player : players.values()){
			player.tick();
		}
		for(Npc npc : npcs.values()){
			npc.tick();
		}
	}

	public Tile getTileAt(IntPoint target) {
		int x = target.getX()/18;
		int y = target.getY()/18;
		return tiles[x][y];

	}
}
